package com.nulldev.util.security.encryptAPI_rw.impls.lea;

import com.nulldev.util.security.encryptAPI_rw.api.ICipherType;
import com.nulldev.util.security.encryptAPI_rw.api.IStandardBlockingCipherWithIV;
import com.nulldev.util.security.encryptAPI_rw.impls.lea.internal.BlockCipher.Mode;
import com.nulldev.util.security.encryptAPI_rw.impls.lea.internal.mode.CTRMode;
import com.nulldev.util.security.encryptAPI_rw.impls.lea.internal.symm.LEA;

public class LEACTRCipher implements IStandardBlockingCipherWithIV {
	private static final String[] CIPHER_NAMES =
		{ "LEA", "lea", "LEA-CTR", "lea-ctr" };
	private static final byte[] BLANK_IV = new byte[16];

	@Override
	public String getEncryptorID() {
		return "ecr_v3.internal.lea.ctr";
	}

	@Override
	public String[] getCipherNames() {
		return CIPHER_NAMES;
	}

	@Override
	public byte[] encrypt(final byte[] data, final byte[] key, final byte[] iv) {
		final CTRMode ctr = new CTRMode(LEA.getEngine());
		ctr.init(Mode.ENCRYPT, key, iv);
		return ctr.doFinal(data);
	}

	@Override
	public byte[] decrypt(final byte[] data, final byte[] key, final byte[] iv) {
		final CTRMode ctr = new CTRMode(LEA.getEngine());
		ctr.init(Mode.DECRYPT, key, iv);
		return ctr.doFinal(data);
	}

	@Override
	public ICipherType getCipherType() {
		return ICipherType.PRIVATE_KEY_IV;
	}

	@Override
	public byte[] encrypt(final byte[] data, final byte[] key) {
		return encrypt(data, key, BLANK_IV);
	}

	@Override
	public byte[] decrypt(final byte[] data, final byte[] key) {
		return decrypt(data, key, BLANK_IV);
	}
}
